/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import postcss from "./postcss.js";

const forNumber = 10000;

function AtRule_test() {
    let rule;
    let startTime = Date.now();
    for (let i = 0; i < forNumber; i++) {
        rule = new postcss.AtRule({ name: 'encoding', params: '"utf-8"' });
    }
    let endTime = Date.now();
    if (rule.name != 'encoding') {
        throw new Error("Inconsistent with expected results")
    }
    print(`postCss_AtRule: ${endTime - startTime} ms`)
}

function Comment_test() {
    let comment;
    let startTime = Date.now();
    for (let i = 0; i < forNumber; i++) {
        comment = new postcss.Comment({ text: 'hi' });
    }
    let endTime = Date.now();
    if (comment.toString() != "/* hi */") {
        throw new Error("Inconsistent with expected results")
    }
    print(`postCss_Comment: ${endTime - startTime} ms`)
}

function Declaration_test() {
    let decl;
    let startTime = Date.now();
    for (let i = 0; i < forNumber; i++) {
        decl = new postcss.Declaration({ prop: 'color', value: 'black' })
    }
    let endTime = Date.now();
    if (decl.prop != "color") {
        throw new Error("Inconsistent with expected results")
    }
    print(`postCss_Declaration: ${endTime - startTime} ms`)
}

function listSpace_test() {
    let list;
    let startTime = Date.now();
    for (let i = 0; i < forNumber; i++) {
        list = postcss.list.space('a b c d e f g ewf vre we 8d dec fer ver')
    }
    let endTime = Date.now();
    if (list != "a,b,c,d,e,f,g,ewf,vre,we,8d,dec,fer,ver") {
        throw new Error("Inconsistent with expected results")
    }
    print(`postCss_List: ${endTime - startTime} ms`)
}
function Rule_test() {
    let rule
    let startTime = Date.now();
    for (let i = 0; i < forNumber; i++) {
        rule = new postcss.Rule({ selector: 'a' })
    }
    let endTime = Date.now();
    let error = rule.error('Test')
    if (error.message != "<css input>: Test") {
        throw new Error("Inconsistent with expected results")
    }
    print(`postCss_Rule: ${endTime - startTime} ms`)
}


let loopCountForPreheat = 1;
for (let i = 0; i < loopCountForPreheat; i++) {
    AtRule_test()
    Comment_test()
    Declaration_test()
    listSpace_test()
    Rule_test()
}

ArkTools.waitAllJitCompileFinish();

AtRule_test()
Comment_test()
Declaration_test()
listSpace_test()
Rule_test()